package com.sxy.service;

import com.sxy.mapper.BrandMapper;
import com.sxy.pojo.Brand;
import com.sxy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class BrandService {
    private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public List<Brand> selectAll(){
        //创建sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建mapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //执行service层方法
        List<Brand> brands = mapper.selectAll();
        //关闭资源
        sqlSession.close();
        //返回集合
        return brands;
    }
    public boolean add(Brand brand){
        //创建sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建mapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //执行service层方法
        boolean flag = mapper.add(brand);

        //提交事务
        sqlSession.commit();
        //关闭资源
        sqlSession.close();
        //返回集合
        return flag;
    }

}
